import urlConfig from './config.js'
const request = {}
request.globalRequest = (url, method, data) => {
  const app_token = uni.getStorageSync('token')
  const headers = {token: app_token}
    return uni.request({
		url: urlConfig + url,
		method,
		data: data,
        dataType: 'json',
        header: headers
    }).then(res => {
        if (res[1].data.code && res[1].data.code == '200') {
			// console.log(res[1])
            return res[1].data
        } else {
            badrequest(res[1].data.code)
			return false
        }
    }).catch(parmas => {
		switch (parmas.code) {
			case 401:
        uni.clearStorageSync()
				uni.navigateTo({
				    url: '../login/login'
				})
				break
			default:
				uni.showToast({
					title: parmas.message,
					icon: 'none'
				})
				return Promise.reject()
				break
		}
	})
}
// 错误码处理
const badrequest = code => {
	if (code) {
		switch (code){
			case '401':
        uni.clearStorageSync()
				uni.navigateTo({
					url: '../login/login'
				})
				break;
			default:
				break;
		}
	}
}
export default request
